import Vue from 'vue'
import VueRouter from 'vue-router'
import Store from '@/store'
import { getToken } from '@/utils/local.js'
import { auInfo } from '@/api/userInfo.js'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    alias: '/login',
    component: () => import('@/views/login/login.vue'),
    meta: {
      needLogin: false
    }
  },
  {
    path: '/home',
    component: () => import('@/views/home/home.vue'),
    redirect: '/home/user',
    children: [
      {
        path: '/home/company', // 写/相当于完整路由,不写 (a=>/home/a),它是相对,相对于home
        component: () => import('@/views/home/company/company.vue'),
        meta: {
          needTab: true
        }
      },
      {
        path: '/home/question',
        component: () => import('@/views/home/question/question.vue'),
        meta: {
          needLogin: true,
          needTab: true
        }
      },
      {
        path: '/home/questionInfo',
        component: () => import('@/views/home/question/questionInfo.vue'),
        meta: {
          needLogin: true
        }
      },
      {
        path: '/home/find',
        component: () => import('@/views/home/find/find.vue'),
        meta: {
          needTab: true
        }
      },
      {
        path: '/home/shareList',
        component: () => import('@/views/home/find/shareList.vue')
      },
      {
        path: '/home/shareInfo/:id?',
        component: () => import('@/views/home/find/shareInfo.vue')
      },
      {
        path: '/home/user',
        component: () => import('@/views/home/user/user.vue'),
        meta: {
          needLogin: true,
          needTab: true
        }
      },
      {
        path: '/home/userInfo', // 写/相当于完整路由,不写 (a=>/home/a),它是相对,相对于home
        component: () => import('@/views/home/user/userInfo.vue'),
        meta: {
          needLogin: true
        }
      },
      {
        path: '/home/editInfo', // 写/相当于完整路由,不写 (a=>/home/a),它是相对,相对于home
        component: () => import('@/views/home/user/editInfo.vue'),
        meta: {
          needLogin: true
        }
      }
    ]
  }
]
const router = new VueRouter({
  routes
})
// 加入前置守卫
router.beforeEach(async (to, from, next) => {
  // window.console.log('to:', to)
  // 记录滚动条位置
  const _top =
    document.documentElement.scrollTop ||
    window.pageYOffset ||
    document.body.scrollTop
  // 保存到路由元信息
  from.meta.scrollTop = _top
  // 调用取消方法，并全局取消所有接口
  window.cancelEvent('', true)
  // 路由元信息是否为true
  if (to.meta.needLogin) {
    // 是否有登录状态
    if (Store.state.isLogin) {
      next()
    } else {
      // 没有登录但是有token
      if (getToken()) {
        /*
        1:保存一个回跳地址
        2:在响应拦截获取
        */
        // Store.commit('setRedirectPath', to.fullPath)
        const res = await auInfo()
        Store.commit('setUserInfo', res.data.data)
        Store.commit('setLoginState', true)
        // window.console.log(res)
        next()
      } else {
        // 它想去to.fullPath(fullPath带了query参数) 但是现在没登陆,希望登陆完成后跳转到to的页面
        next('/login?redirect=' + to.fullPath)
      }
    }
  } else {
    // 不需要登录
    next()
    // 有token且没有登录
    if (getToken() && !Store.state.isLogin) {
      // 不处理错误
      const res = await auInfo(true)
      Store.commit('setUserInfo', res.data.data)
      // 在导航守卫中不需要登录页面，如果满足登录条件就登录
      // 并传了一个参数，如果调用该接口出现错误不要处理
      Store.commit('setLoginState', true)
    }
  }
})
// 加入后置守卫
router.afterEach((to, from) => {
  window.scrollTo(0, 0)
})
// 路由同一地址发生错误解决方法，百度
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}
export default router
